<html>
<head><meta charset="utf-8"><title>splitting impls for traits · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/splitting.20impls.20for.20traits.html">splitting impls for traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226426386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/splitting%20impls%20for%20traits/near/226426386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/splitting.20impls.20for.20traits.html#226426386">(Feb 15 2021 at 20:19)</a>:</h4>
<p>Hello! </p>
<p>I use macros in my project a lot to declare stuff, however, i would like to be able to split a trait implementation across multiple blocks for convenience. I was wondering if this has ever been proposed before? e.g.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">();</span><span class="w"></span>
<span class="w"> </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The biggest downsides i can see in this is that it adds complexity to rustc and maybe slows things down since it has to crawl to find other impl blocks and see if they satisfy all functions. It also makes errors a little bit weirder.</p>



<a name="226429594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/splitting%20impls%20for%20traits/near/226429594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/splitting.20impls.20for.20traits.html#226429594">(Feb 15 2021 at 21:01)</a>:</h4>
<p>this sounds sort of like <code>partial class</code> in C#</p>



<a name="226429663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/splitting%20impls%20for%20traits/near/226429663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/splitting.20impls.20for.20traits.html#226429663">(Feb 15 2021 at 21:02)</a>:</h4>
<p>can you expand on why you want to split the blocks? You can do that for normal impl blocks, just not currently for traits</p>



<a name="226437785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/splitting%20impls%20for%20traits/near/226437785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Riccardo D&#x27;Ambrosio <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/splitting.20impls.20for.20traits.html#226437785">(Feb 15 2021 at 22:29)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> so i am making a linter, and i have a macro for declaring rules, the macro implements <code>Rule</code> plus <code>CstRule</code>. Now, i don't really want to add more stuff to this macro for declaring tags (which say if a rule is reccomended and other stuff), id rather it be a proc macro on the struct. However, i cannot do this if i cant split the impl for <code>CstRule</code> between what the macro makes and what the proc macro makes.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>